class Iori extends imageClass{
    constructor(game){
        super(game, "stand1")
        this.game = game
        this.setup()
    }

    setup(){
        this.frameCount = config.action_speed
        this.frameIndex = 0
        this.animations = {stand:[], forward:[], jump:[], down:[], back:[], jump_forward:[], run:[]}
        this.x = 150
        this.y = 130
        this.joystick = "stand"
        this.inversion = false
        this.actChange = false
        this.action = "stand"
        this.keys = []

        for (var e in this.animations){
            this.readFrames(e)
        }
    }

    readFrames(action){
        for (var i = 1; i <= kofFrame[action].count; i ++){
            var name = action + `${i}`
            var t = this.game.textureByName(name)
            this.animations[action].push(t)
        }
    }

    draw(){
        self = this
        log("[draw] x:", self.x, "y:", self.y, "texture:", self.texture, "actChange:", self.isActionChange())
        self.game.ctx.drawImage(self.texture, self.x,self.y)
    }

    pic(action){
        return kofFrame[this.action]
    }

    // 上、下、左、右
    jump(){
        self = this
        self.joystick = "jump"
    }
    down(){
        self = this
        self.joystick = "down"
            self.y = 150
    }
    moveLeft ()  {
        self = this
        if (!self.isActionChange()){
            self.joystick = "back"
            self.frameCount = 1
        }else{
            self.y = 130
            if (self.x >= 0){
                self.x -= config.player_speed
            }
        }
    }
    moveRight (){
        self = this
        if (!self.isActionChange()){
            self.joystick = "forward"
            self.frameCount = 1
        }else{
            self.y = 130
            if (self.x <= 752 - self.pic(self.action).w){
                self.x += config.player_speed
            }
        }
    }

    // 跑、前跳、后跳
    jump_forward(){
        log("jump_forward")
    }
    run(){
        self.y = 130
        log("run")
    }


    setTrick(name){
        this.action = name
    }

    isActionChange(){
        return (this.last_action != this.action) || (this.last_joystike != this.joystick)
    }

    frame(){
        return this.animations[this.action][this.frameIndex]
    }

    setFrames(){
        var self = this 
        self.frameCount -- 
        if (self.frameCount == 0){
            self.frameCount = config.action_speed
            self.frameIndex = (self.isActionChange()) ? 
                                0 : (self.frameIndex + 1) % kofFrame[self.action].count
            self.texture = self.frame()
        }
    }

    trick(){
        self = this
        let pKeys = self.game.keys.toString().replace(/,/g, "")
        log(pKeys)
        for (let key in kofFrame){
            let action = kofFrame[key].keys
            if (pKeys.indexOf(action)!=-1){
                return key
            }
        }
        return false
    }

    update(){
        self = this
        self.setTrick(self.trick() || self.joystick)
        //log(self.action)

        self.setFrames()
        self.y = 130
        self.last_action = self.action
        self.last_joystike = self.joystick
    }
}
